using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using JigLibX.Geometry;
using JigLibX.Physics;
using JigLibX.Math;
using JigLibX.Collision;

namespace RPG
{
    public enum GameState
    {
        INITIAL_LOADING = 0,
        MENU = 1,
        INGAME = 2,
        EXITING = 3
    }

    public class CGameMain : Microsoft.Xna.Framework.Game
    {
        public int gameState;

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        CGameEntity e;
        CGameCamera camera;

        CGameGUIRenderer guiRenderer;

        PhysicsSystem p;
        CollisionSystem c;

        public int SelfIndex;

        public List<CGamePlayer> Players;

        public CGameMain()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            gameState = 0;

            spriteBatch = new SpriteBatch(GraphicsDevice);

            Primitives2D.dot = Content.Load<Texture2D>("dot");

            guiRenderer = new CGameGUIRenderer(this);

            p = new PhysicsSystem();
            c = new CollisionSystemSAP();

            p.CollisionSystem = c;
            
            e = new CGameEntity(new Vector3(0, 0, 0), 100, new Vector3(20, 1, 20));
            e.Model = Content.Load<Model>("cube");
            e.Body.Immovable = true;

            Players = new List<CGamePlayer>();

            CGameEntity ch = new CGameEntity(Vector3.Zero, 1, Vector3.One);
            ch.Model = Content.Load<Model>("cube");
            CGamePlayer pl = new CGamePlayer(ch);
            CGameController cl = new CGameController(pl);

            Players.Add(pl);
            SelfIndex = Players.IndexOf(pl);

            camera = new CGameChaseCamera(graphics, ch);
            gameState = 2;
        }

        protected override void UnloadContent()
        {

        }

        public MouseState newMouseState;
        public MouseState oldMouseState;

        public KeyboardState newKS;
        public KeyboardState oldKS;

        protected override void Update(GameTime gameTime)
        {
            oldMouseState = newMouseState;
            newMouseState = Mouse.GetState();

            oldKS = newKS;
            newKS = Keyboard.GetState();
            

            camera.Update(gameTime);

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            float timeStep = (float)gameTime.ElapsedGameTime.Ticks / TimeSpan.TicksPerSecond;

            p.Integrate(timeStep);
            camera.Update(gameTime);

            guiRenderer.Update(gameTime);

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            e.Draw(gameTime, camera);
            Players[SelfIndex].Entity.Draw(gameTime, camera);

            guiRenderer.Draw(spriteBatch);

            base.Draw(gameTime);
        }
    }
}
